<!--
	- EditAnalysisLib.php
	- v0.04
	- 4/2/2011
	-
	- This contains the functions for updating the costs of the analysis in the database
	-->
<?php

/* Function for updating the Crude Protein Type */	
function editCrudeProtein($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $factor,  $year, 
			  $sample, $id1, $id2, $id3, $id4, $database, $host, $login, 
			  $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");
		if (!$connect) {
			die ('Could not connect: ' . mysql_error());
		}
		mysql_select_db ($database, $connect);		
		/* Updates the database */
		$result1 = mysql_query ("UPDATE edit_analysis SET $val = '$A1'
							WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query ("UPDATE edit_analysis SET $val = '$B1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query ("UPDATE edit_analysis SET $val = '$C1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id3'");
		$result4 = mysql_query ("UPDATE edit_analysis SET $val = '$factor' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id4'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close ($connect);
		
		if ($result1 == FALSE || $result2 == FALSE || $result3 == FALSE || 
			$result4 == FALSE) {
				return false;
		} else if ($result1 == TRUE && $result2 == TRUE && $result3 == TRUE && 
					  $result4 == TRUE) {
							return true;
		}
}

/* Function for updating the AshType */	
function editAsh($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $year, $sample, $id1, $id2, 
			   $id3, $database, $host, $login, $loginpassword){
		$connect = mysql_connect("localhost","editanalysis","editanalysis");
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);
		 /*Updates the database*/ 
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1' 
						   WHERE casl_year = '$year' AND
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id3'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

/* Function for updating the Crude Fat Type */	
function editCrudeFat($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $year, $sample, $id1, $id2, $id3,
			   $database, $host, $login, $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);		
		/* Updates the database */
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
							WHERE casl_year = '$year' AND 
							casl_sample = '$sample' AND parameter_id = '$id3'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

/* Function for updating the Crude Fiber Type */	
function editCrudeFiber($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $year, $sample, $id1, $id2, 
				$id3, $database, $host, $login, $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");	
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);
		/* Updates the database */
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
							WHERE casl_year = '$year' AND 
							casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
							WHERE casl_year = '$year' AND 
							casl_sample = '$sample' AND parameter_id = '$id3'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

/* Function for updating the Moisture Type */	
function editMoisture($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $year, $sample, $id1, $id2,
				$id3, $database, $host, $login, $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");	
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);			
		/* Updates the database */
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
						   WHERE casl_year = '$year'
						   AND casl_sample = '$sample' AND parameter_id = '$id3'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

/* Function for updating the Carbohydrate Type */	
function editCarbohydrate($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $D1, $E1, 
				$year, $sample, $id1, $id2, $id3, $id4, $id5, $database, $host,
				$login, $loginpassword){

		$connect = mysql_connect("localhost","editanalysis","editanalysis");	
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);
		/* Updates the database */
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id3'");
		$result4 = mysql_query("UPDATE edit_analysis SET $val = '$D1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id4'");
		$result5 = mysql_query("UPDATE edit_analysis SET $val = '$E1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id5'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'percent' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE || 
			$result4 == FALSE || $result5 == FALSE){
				return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE && 
					$result4 == TRUE && $result5 == TRUE){
						return true;
		}
}

/* Function for updating the Total Energy Type */	
function editTotalEnergy($standardDeviation, $mean, $resultVal, $compute, $val, $A1, $B1, $C1, $year, $sample, $id1,
				$id2, $id3, $database, $host, $login, $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);
		/* Updates the database */
		$result1 = mysql_query("UPDATE edit_analysis SET $val = '$A1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id1'");
		$result2 = mysql_query("UPDATE edit_analysis SET $val = '$B1'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id2'");
		$result3 = mysql_query("UPDATE edit_analysis SET $val = '$C1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample' AND parameter_id = '$id3'");
		$final = mysql_query("UPDATE analysis_result SET $resultVal = '$compute', 
						   unit = 'kcal/100g' WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

function editOthers($mean, $standardDeviation, $resultVal1, $val1, $resultVal2, $val2, $resultVal3, $val3, $year, $sample, $database, $host, $login, $loginpassword){
		
		$connect = mysql_connect("localhost","editanalysis","editanalysis");
		if (!$connect) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($database, $connect);
		/* Updates the database */
		$result1 = mysql_query("UPDATE analysis_result SET $resultVal1 = '$val1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$result2 = mysql_query("UPDATE analysis_result SET $resultVal2 = '$val2' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$result3 = mysql_query("UPDATE analysis_result SET $resultVal3 = '$val3' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$mean = mysql_query("UPDATE analysis_result SET mean = '$mean'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$standardDeviation = mysql_query("UPDATE analysis_result SET standard_deviation = '$standardDeviation'
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$result4 = mysql_query("UPDATE edit_analysis  SET value1 = '$val1' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$result5 = mysql_query("UPDATE edit_analysis SET value2 = '$val2' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		$result6 = mysql_query("UPDATE edit_analysis SET value3 = '$val3' 
						   WHERE casl_year = '$year' AND 
						   casl_sample = '$sample'");
		mysql_close($connect);
		
		if($result1 == FALSE || $result2 == FALSE || $result3 == FALSE){
			return false;
		}else if($result1 == TRUE && $result2 == TRUE && $result3 == TRUE){
			return true;
		}
}

function getLimitedTable($year, $no, $mainType, $sampleType, $analysisRequired, $database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT analysis_type FROM analysis WHERE main_type = '$mainType' AND sample_type = '$sampleType' AND analysis_type = '$analysisRequired'")or die(mysql_error());
	$row = mysql_fetch_array($result);
	mysql_close($con);
	return $row[0];
}

function getID($year, $no, $mainType, $sampleType, $analysisRequired, $database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT id FROM analysis WHERE main_type = '$mainType' AND sample_type = '$sampleType' AND analysis_type = '$analysisRequired'")or die(mysql_error());
	$row = mysql_fetch_array($result);
	mysql_close($con);
	return $row[0];
}

function getValue($val, $id, $year, $no, $database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT $val FROM edit_analysis WHERE parameter_id = '$id' AND casl_year = '$year' AND casl_sample = '$no'")or die(mysql_error());
	$row = mysql_fetch_array($result);
	mysql_close($con);
	return $row[0];
}

function getMean($year, $no, $database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT mean FROM analysis_result WHERE casl_year = '$year' AND casl_sample = '$no'")or die(mysql_error());
	$row = mysql_fetch_array($result);
	mysql_close($con);
	return $row[0];
}

function getStandardDev($year, $no, $database,$host,$login,$loginPassword){
	$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
	mysql_select_db($database) or die(mysql_error());
	$result = mysql_query("SELECT standard_deviation FROM analysis_result WHERE casl_year = '$year' AND casl_sample = '$no'")or die(mysql_error());
	$row = mysql_fetch_array($result);
	mysql_close($con);
	return $row[0];
}
?>